package com.sw.bbs.board.service.impl;

import com.sw.bbs.board.dao.BoardMapper;
import com.sw.bbs.board.dao.PostMapper;
import com.sw.bbs.board.dao.media.TagMapper;
import com.sw.bbs.board.pojo.Board;
import com.sw.bbs.board.pojo.Post;
import com.sw.bbs.board.pojo.Tag;
import com.sw.bbs.board.service.*;
import com.sw.bbs.board.vo.request.IndexSearchRequest;
import com.sw.bbs.board.vo.request.board.QueryBoardRequest;
import com.sw.bbs.board.vo.request.comment.QueryCommentRequest;
import com.sw.bbs.board.vo.request.post.QueryPostRequest;
import com.sw.bbs.board.vo.response.FollowBoardPostResponse;
import com.sw.bbs.board.vo.response.IndexSearchResponse;
import com.sw.bbs.board.vo.response.board.BoardOperaResponse;
import com.sw.bbs.board.vo.response.post.PostListResponse;
import com.sw.bbs.user.pojo.User;
import com.sw.bbs.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Desc: No Desc
 * @Author: wusonghe
 * @Date: 2019/4/1 16:08
 */
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private BoardMapper boardMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    /**
     * 全网搜索
     *
     * @param request
     * @return
     */
    @Override
    public IndexSearchResponse searchForAll(IndexSearchRequest request) {

        String[] keywords = request.getKeyword().split(" ");
        tagService.updateAndInsertTags(Arrays.asList(keywords));

        //查询
        IndexSearchResponse searchResponse = new IndexSearchResponse();

        QueryBoardRequest queryBoardRequest = new QueryBoardRequest();
        queryBoardRequest.setOnceQuery(1);
        queryBoardRequest.setKeyword(request.getKeyword());
        List<Board> boardes = boardMapper.queryQueryListResponse(queryBoardRequest);
        searchResponse.setBoards(boardes);

        QueryPostRequest queryPostRequest = new QueryPostRequest();
        queryPostRequest.setOnceQuery(1);
        queryPostRequest.setKeyword(request.getKeyword());
        List<PostListResponse> posts = postMapper.queryPostList(queryPostRequest);
        searchResponse.setPosts(posts);

        return searchResponse;
    }

    @Override
    public List<FollowBoardPostResponse> queryFollowBoardPost(HttpServletRequest httpServletRequest) {

        User user = userService.getUserByRequest(httpServletRequest);
        List<FollowBoardPostResponse> followBoardPostResponseList = new ArrayList<>();
        List<BoardOperaResponse> boardList = boardMapper.queryProfileBoardsByType(user.getUserId(),3);
        for (BoardOperaResponse boardOperaResponse: boardList) {

            FollowBoardPostResponse followBoardPostResponse = new FollowBoardPostResponse();
            followBoardPostResponse.setBoardId(boardOperaResponse.getBoardId());
            followBoardPostResponse.setTitle(boardOperaResponse.getTitle());

            QueryPostRequest queryPostRequest = new QueryPostRequest();
            queryPostRequest.setOnceQuery(1);
            queryPostRequest.setBoardId(boardOperaResponse.getBoardId());
            List<PostListResponse> postList = postMapper.queryPostList(queryPostRequest);
            followBoardPostResponse.setPosts(postList);

            followBoardPostResponseList.add(followBoardPostResponse);

        }
        return followBoardPostResponseList;


    }
}
